﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace IP2Client
{
    public partial class PatientHistoryChart : Form
    {
        private List<Measurement> measurementList = new List<Measurement>();       

        /// <summary>
        /// Constructor for PatientHistoryChart
        /// </summary>
        /// <param name="selectedPatient">String selectedPatient</param>
        /// <param name="date">String date</param>
        /// <param name="file">String File</param>
        public PatientHistoryChart(string selectedPatient, string date, string file)
        {
            InitializeComponent();

            measurementChart.Series[0].ChartType = SeriesChartType.FastLine;
            measurementChart.Series[0].Color = Color.Red;

            getMeasurementList(selectedPatient, date, file);

            hanleTcpClientUpdates();
        }

        /// <summary>
        /// asks server to send measurement associated with the given patient, date and file
        /// </summary>
        /// <param name="selectedPatient">patient you want the measurements from</param>
        /// <param name="date">date on which this measurements are saved</param>
        /// <param name="file">the fileName</param>
        public void getMeasurementList(string selectedPatient, string date, string file)
        {
            ConnectionHandler.SendObject("!sndSessionFile" + " " + selectedPatient + " " + date + " " + file);
        }

        /// <summary>
        /// Handles all the data send from the server to this application    
        /// </summary>
        public void hanleTcpClientUpdates()
        {
            object rcvdObject = null;

            if (ConnectionHandler.HandledClient == null)
            {
                return;
            }

            rcvdObject = ConnectionHandler.ReadObject();

            measurementList = rcvdObject as List<Measurement>;

            if (measurementList != null && measurementList.Count != 0)
            {
                setChart("RPM");
            }
        }

        /// <summary>
        /// makes sure the chart is drawn
        /// </summary>
        private void setChart(string checkedOption)
        {
            int intMinY = 0;
            int intMaxY = 0;

            measurementChart.Series[0].Points.Clear();

            //could be removed by adding these min and max values to the parameter section
            switch (checkedOption)
            {
                case "RPM":
                    intMinY = 0;

                    int maxValue = int.MinValue;
                    foreach (Measurement m in measurementList)
                    {
                        if (m.Rpm > maxValue)
                        {
                            maxValue = m.Rpm;
                        }
                    }

                    intMaxY = maxValue + 10;
                    measurementChart.Series[0].Name = "Rounds Per Minute";
                    break;
                case "SPEED":
                    intMinY = 0;
                    intMaxY = 75;
                    measurementChart.Series[0].Name = "Speed";
                    break;
                case "HEART":
                    intMinY = 50;
                    intMaxY = 210;
                    measurementChart.Series[0].Name = "Heartrate";
                    break;
                case "ENERGY":
                    intMinY = 0;
                    intMaxY = 250;
                    measurementChart.Series[0].Name = "Energy";
                    break;
                case "CURRENTRESISTANCE":
                    intMinY = 0;
                    intMaxY = 400;
                    measurementChart.Series[0].Name = "Current Resistance";
                    break;
                case "SETRESISTANCE":
                    intMinY = 0;
                    intMaxY = 400;
                    measurementChart.Series[0].Name = "Set Resistance";
                    break;
                default:
                    break;
            }

            measurementChart.ChartAreas[0].AxisY.Minimum = intMinY;
            measurementChart.ChartAreas[0].AxisY.Maximum = intMaxY;


            int intTijdsIndeling = 2;
            foreach (Measurement m in measurementList)
            {
                switch (checkedOption)
                {
                    case "RPM":
                        measurementChart.Series[0].Points.AddXY(intTijdsIndeling / 2, m.Rpm);      
                        break;
                    case "SPEED":
                        measurementChart.Series[0].Points.AddXY(intTijdsIndeling / 2, (int)m.Speed);
                        break;
                    case "HEART":
                        measurementChart.Series[0].Points.AddXY(intTijdsIndeling / 2, m.HeartBeats);
                        break;
                    case "ENERGY":
                        measurementChart.Series[0].Points.AddXY(intTijdsIndeling / 2, m.Energy);
                        break;
                    case "CURRENTRESISTANCE":
                        measurementChart.Series[0].Points.AddXY(intTijdsIndeling / 2, m.CurrentPower);
                        break;
                    case "SETRESISTANCE":
                        measurementChart.Series[0].Points.AddXY(intTijdsIndeling / 2, m.Power);
                        break;
                    default:
                        break;
                }
                intTijdsIndeling++;
            }

        }

        /// <summary>
        /// Event triggered by clicking the RPM radio button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rpmRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            setChart("RPM");
        }
        
        /// <summary>
        /// Event triggered by clicking the SPEED radio button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void speedRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            setChart("SPEED");
        }

        /// <summary>
        /// Event triggered by clicking the HEART radio button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void heartrateRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            setChart("HEART");
        }

        /// <summary>
        /// Event triggered by clicking the ENERGY radio button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EnergyRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            setChart("ENERGY");
        }

        /// <summary>
        /// Event triggered by clicking the Current Resistance radio button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void resCurRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            setChart("CURRENTRESISTANCE");
        }

        /// <summary>
        /// Event triggered by clicking the Set Resistance radio button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void resSetRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            setChart("SETRESISTANCE");
        }
    }
}
